<?php /* УТФ-8:fix */

class SPImodel__feedback__items__front extends SPImodel__feedback__items
{
    public function select( $select_offset, $select_limit )
    {
        $md5_key = __METHOD__."_".md5(serialize(func_get_args()));
		
		if( ($val = SPI::I('memcache')->get_list('feedback_items', $md5_key)) !== false){//echo"<h3>$md5_key - из кеша</h3>";///////////////////////////////
			return $val;
		}
		
		$query =    " SELECT id"
               .      " FROM ".SPIcfg::$store['table:feedback_items']
               .     " WHERE is_public=1 "
               . " ORDER BY t_created DESC, id DESC "
               .    " LIMIT " . (int) $select_limit
               .   " OFFSET " . (int) $select_offset
               ;
		
		$qresult = SPIstore::I()->query( $query );
		$res = array();
		while ($item = $qresult -> fetch()){
			$res[] = $item['id'];
		}
		
		SPI::I('memcache')->save_list('feedback_items', $md5_key, $res);
		
		return $res;
        //return SPIstore::I()->query( $query );
    }

    public function select_count_after()
    {
		$md5_key = __METHOD__."_".md5(serialize(func_get_args()));
		
		if( ($val = SPI::I('memcache')->get_list('feedback_items', $md5_key)) !== false){//echo"<h3>$md5_key - из кеша</h3>";///////////////////////////////
			return $val;
		}
        $query =    " SELECT COUNT(*) AS count "
               .      " FROM ".SPIcfg::$store['table:feedback_items']
               .     " WHERE is_public=1 "
               ;

        $item = SPIstore::I()->query( $query )->fetch_and_close();
		
		SPI::I('memcache')->save_list('feedback_items', $md5_key, $item['count']);
		
        return $item['count'];
    }

    public function insert( $name, $email, $message )
    {	
		$list_duild = SPI::I('memcache')->get_list_build('feedback_items', true);
	
        $fields = array();
        //$fields['pid_feedback_opt'] = (int) $pid_feedback_opt;
        $fields['name'] = $name;
        $fields['email'] = $email;
        $fields['message'] = $message;
        $fields['user_ip'] = ip2long( SPI::I('util')->get_user_ip() );

        $pid_profile = SPI::I('Iam')->get_id();
        if( $pid_profile )
        {
            $fields['pid_profile'] = $pid_profile;
        }

        $result = SPIstore::I()->insert(
            SPIcfg::$store['table:feedback_items'],
            $fields,
            null,
            array( 'id' => SPIcfg::$store['sequence:global'] )
        );

        return $result === false ? false : $result['id'];
    }

    public function get_by_id( $item_id )
    {
		$key = md5(serialize((int)$item_id));
		
		if( ($val = SPI::I('memcache')->get($key)) !== false){
			return $val;
		}
        $query =    " SELECT * "
               .      " FROM ".SPIcfg::$store['table:feedback_items']
               .     " WHERE is_public=1 "
               .       " AND id=" . (int) $item_id
               ;

        $res = SPIstore::I()->query( $query )->fetch_and_close();
		
		SPI::I('memcache')->set($key, $res);

        return $res;
    }
}
